package com.lsfb.light.Index;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;

import com.gyf.barlibrary.ImmersionBar;
import com.lsfb.light.BusinessService.ServiceFragment;
import com.lsfb.light.Home.HomeFragment;
import com.lsfb.light.Light.LightFragment;
import com.lsfb.light.Mine.MineFragment;
import com.lsfb.light.R;
import com.lsfb.light.Utils.LsfbUtils;
import com.lsfb.light.View.MyFragmentActivity;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.view.bottombar.BottomBarItem;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends MyFragmentActivity {
    private BottomBarItem bar1, bar2, bar3, bar4;
    private List<Fragment> mFragments;

    private ImmersionBar immersionBar;

    private int ButtonBarHeight = 0;

    private BroadcastReceiver mReceiver;
    private IntentFilter intentFilter;
    private String Action_SSCMsg = "com.lsfb.light";

    @Override
    public void initView() {
        LsfbLog.e("测试 10");
        LsfbUtils.initImagePath(this);
        LsfbEvent.getInstantiation().register2(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        immersionBar = ImmersionBar.with(this);
        immersionBar.statusBarColor(R.color.theme_color).init();
        setIsScroll(false);
        LsfbEvent.getInstantiation().register2(this);

        ColorStateList cls = getResources().getColorStateList(R.color.btn_index_bottom);
        Rect rect = new Rect(0, 0, DensityUtils.dp2px(this, 20), DensityUtils.dp2px(this, 20));

        ArrayList list = new ArrayList<BottomBarItem>();

        bar1 = new BottomBarItem();
        bar1.setId(View.generateViewId());
        bar1.setColorStateList(cls);
        bar1.setName("首页");
        bar1.setIsCheck(true);
        bar1.setImgRes(R.drawable.bottom_home);
        list.add(bar1);

        bar2 = new BottomBarItem();
        bar2.setId(View.generateViewId());
        bar2.setName("红绿灯");
        bar2.setColorStateList(cls);
        bar2.setImgRes(R.drawable.bottom_light);
        list.add(bar2);

        bar3 = new BottomBarItem();
        bar3.setId(View.generateViewId());
        bar3.setName("商务服务");
        bar3.setColorStateList(cls);
        bar3.setImgRes(R.drawable.bottom_service);
        list.add(bar3);

        bar4 = new BottomBarItem();
        bar4.setId(View.generateViewId());
        bar4.setName("个人中心");
        bar4.setColorStateList(cls);
        bar4.setImgRes(R.drawable.bottom_mine);
        list.add(bar4);

        bar1.setBounds(rect);
        bar2.setBounds(rect);
        bar3.setBounds(rect);
        bar4.setBounds(rect);

        getButtonBar().setPadding(0, 16, 0, 16);
        getButtonBar().initButtonBar(list, R.color.white_color);
        mFragments = new ArrayList<>();
        setMainBackground();
        initFragments();
    }

    /**
     * Fragment添加内容
     */
    public void initFragments() {
        //首页
        mFragments.add(new HomeFragment());
        //红绿灯
        mFragments.add(new LightFragment());
        //商务服务
        mFragments.add(new ServiceFragment());
        //个人中心
        mFragments.add(new MineFragment());

        setTabFragments(mFragments);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void tabOnClick(View view, ViewPager viewPager) {
        int id = view.getId();
        if (id == bar1.getId()) {
            viewPager.setCurrentItem(0);
        } else if (id == bar2.getId()) {
            viewPager.setCurrentItem(1);
        } else if (id == bar3.getId()) {
            viewPager.setCurrentItem(2);
        } else if (id == bar4.getId()) {
            viewPager.setCurrentItem(3);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initbarset();
    }

    private void initbarset() {
        switch (viewPager.getCurrentItem()) {
            case 0:
                getButtonBar().setCheckById(bar1.getId());
                break;
            case 1:
                getButtonBar().setCheckById(bar2.getId());
                break;
            case 2:
                getButtonBar().setCheckById(bar3.getId());
                break;
            case 3:
                getButtonBar().setCheckById(bar4.getId());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        immersionBar.destroy();  //在BaseActivity里销毁
        super.onDestroy();
        LsfbEvent.getInstantiation().unregister2(this);
    }
}
